package user

import (
	"context"
	"time"

	"go-weixin/app/usercenter/api/internal/svc"
	"go-weixin/app/usercenter/api/internal/types"
	"go-weixin/app/usercenter/rpc/pb/proto/usercenter"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserGetPowerLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserGetPowerLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserGetPowerLogic {
	return &UserGetPowerLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserGetPowerLogic) UserGetPower(req *types.UserGetPowerReq) (resp *types.UserGetPowerResp, err error) {
	ctx, canelFunc := context.WithTimeout(context.Background(), 5*time.Second)
	defer canelFunc()
	res, err := l.svcCtx.UserRpc.UserGetPower(ctx, &usercenter.UserGetPowerReq{
		Id: req.UserId,
	})
	if err != nil {
		return nil, err
	}
	var powerList []types.Power
	if len(res.PowerList) > 0 {
		for _, power := range res.PowerList {
			powerList = append(powerList, types.Power{
				Id:       power.Id,
				Name:     power.Name,
				Method:   power.Method,
				Url:      power.Url,
				ParentId: power.ParentId,
				CreateAt: power.CreateAt,
				UpdateAt: power.UpdateAt,
			})
		}
	}
	resp = new(types.UserGetPowerResp)
	resp.PowerList = powerList
	return
}
